1 package edu.jiangxin.apktoolbox.file.core;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8 import java.io.InputStreamReader;
9 import java.io.OutputStreamWriter;
10 import java.nio.charset.StandardCharsets;
11 import java.util.Collection;
12 import java.util.Iterator;
13 import java.util.List;
14
15 import org.apache.commons.io.FileUtils;
16 import org.apache.commons.lang3.StringUtils;
17 import org.apache.logging.log4j.LogManager;
18 import org.apache.logging.log4j.Logger;
19
20
21
22
23
24
25
26 public class OsPatternConvert {
27
28 private static final Logger logger = LogManager.getLogger(OsPatternConvert.class.getSimpleName());
29
30
31
32
33
34
35
36
37
38
39
40 private static void convert(String srcFileString, String desFileString, String options) {
41
42
43 String special = ".OSPattenConvert.temp";
44
45 if (srcFileString.equals(desFileString)) {
46 srcFileString = srcFileString + special;
47 FileProcess.copyFile(desFileString, srcFileString);
48 }
49 File srcFileFile = new File(srcFileString);
50 File desFileFile = new File(desFileString);
51
52
53 if (!srcFileFile.exists()) {
54 System.out.println("源文件不存在:" + srcFileFile.getAbsolutePath());
55 return;
56 }
57
58 if (!desFileFile.getParentFile().exists()) {
59 boolean ret = desFileFile.getParentFile().mkdirs();
60 if (!ret) {
61 logger.error("mkdirs failed: " + desFileFile.getParentFile());
62 }
63 }
64
65
66 try (BufferedReader reader = new BufferedReader(
67 new InputStreamReader(new FileInputStream(srcFileFile), StandardCharsets.UTF_8));
68 BufferedWriter writer = new BufferedWriter(
69 new OutputStreamWriter(new FileOutputStream(desFileFile), StandardCharsets.UTF_8))) {
70 String temp = null;
71 while ((temp = reader.readLine()) != null) {
72 writer.write(temp);
73 writer.write(options);
74 }
75 } catch (Exception e) {
76 logger.error("Exception", e);
77 }
78
79
80 if (srcFileString.equals(desFileString + special)) {
81 boolean ret = srcFileFile.delete();
82 if (!ret) {
83 logger.error("delete tmp file failed: " + srcFileFile);
84 }
85 }
86 }
87
88
89
90
91
92
93
94 private static void toUnix(String srcFileString, String desFileString) {
95 convert(srcFileString, desFileString, "\n");
96 logger.info("Success to convert {} to unix", srcFileString);
97 }
98
99
100
101
102
103
104
105 private static void toDos(String srcFileString, String desFileString) {
106 convert(srcFileString, desFileString, "\r\n");
107 logger.info("Success to convert {} to dos", srcFileString);
108 }
109
110
111
112
113
114
115
116 private static void toMac(String srcFileString, String desFileString) {
117 convert(srcFileString, desFileString, "\r");
118 logger.info("Success to convert {} to mac", srcFileString);
119 }
120
121
122
123
124
125
126
127 public static void dos2Unix(String srcFileString, String desFileString) {
128 toUnix(srcFileString, desFileString);
129 }
130
131
132
133
134
135
136
137 public static void dos2Mac(String srcFileString, String desFileString) {
138 toMac(srcFileString, desFileString);
139 }
140
141
142
143
144
145
146
147 public static void unix2Dos(String srcFileString, String desFileString) {
148 toDos(srcFileString, desFileString);
149 }
150
151
152
153
154
155
156
157 public static void unix2Mac(String srcFileString, String desFileString) {
158 toMac(srcFileString, desFileString);
159 }
160
161
162
163
164
165
166
167 public static void mac2Unix(String srcFileString, String desFileString) {
168 toUnix(srcFileString, desFileString);
169 }
170
171
172
173
174
175
176
177 public static void mac2Dos(String srcFileString, String desFileString) {
178 toDos(srcFileString, desFileString);
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 public static void osFileConvert(String srcFileString, String desFileString, String pattern) {
195
196
197 pattern = pattern.toLowerCase();
198
199 pattern = pattern.replace("to", "2");
200
201 pattern = pattern.replace("linux", "unix");
202
203 pattern = pattern.replace("bsd", "unix");
204
205 pattern = pattern.replace("windows", "dos");
206 boolean isToUnix = "2unix".equals(pattern) || "mac2unix".equals(pattern) || "dos2unix".equals(pattern)
207 || "unix2unix".equals(pattern);
208 boolean isToMac = "2mac".equals(pattern) || "dos2mac".equals(pattern) || "unix2mac".equals(pattern);
209 boolean isToDos = "2dos".equals(pattern) || "mac2dos".equals(pattern) || "unix2dos".equals(pattern)
210 || "dos2dos".equals(pattern);
211
212 if (isToUnix) {
213 toUnix(srcFileString, desFileString);
214 } else if (isToDos) {
215 toDos(srcFileString, desFileString);
216 } else if (isToMac) {
217 toMac(srcFileString, desFileString);
218 } else {
219 System.err.println("Error input,can't convert!");
220 }
221 }
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236 public static void osFileConvert(String fileString, String pattern) {
237 osFileConvert(fileString, fileString, pattern);
238 }
239
240
241
242
243
244
245
246
247
248
249
250
251
252 public static void osDirConvert(String srcDirString, String desDirString, String pattern, String suffix) {
253 File srcDirFile = new File(srcDirString);
254 File desDirFile = new File(desDirString);
255 if (!srcDirFile.exists()) {
256 System.out.println("源目录不存在" + srcDirFile.getAbsolutePath());
257 }
258 String[] extensions = null;
259 if (StringUtils.isNotEmpty(suffix)) {
260 extensions = suffix.split(",");
261 }
262 Collection<File> arrayList = FileUtils.listFiles(srcDirFile, extensions, true);
263 Iterator<File> it = arrayList.iterator();
264 while (it.hasNext()) {
265 File srcFileFile = it.next();
266
267 String srcFileString = srcFileFile.getAbsolutePath();
268 String temp = srcFileFile.getAbsolutePath().substring(srcDirFile.getAbsolutePath().length());
269
270 String desFileString = desDirFile.getAbsolutePath() + temp;
271 osFileConvert(srcFileString, desFileString, pattern);
272 }
273
274 }
275
276
277
278
279
280
281
282
283
284
285
286
287 public static void osDirConvert(String srcDirString, String desDirString, String pattern) {
288 osDirConvert(srcDirString, desDirString, pattern, null);
289 }
290
291
292
293
294
295
296
297
298
299
300
301 public static void osConvertFiles(List<File> files, String pattern) {
302 Iterator<File> it = files.iterator();
303 while (it.hasNext()) {
304 osFileConvert(it.next().getAbsolutePath(), pattern);
305 }
306 }
307
308 }